package com.yuliang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuliang.constants.SystemConstants;
import com.yuliang.domain.R;
import com.yuliang.domain.entity.Comment;
import com.yuliang.domain.vo.CommentVo;
import com.yuliang.domain.vo.PageVo;
import com.yuliang.enums.AppHttpCodeEnum;
import com.yuliang.exception.SystemException;
import com.yuliang.mapper.CommentMapper;
import com.yuliang.mapper.UserMapper;
import com.yuliang.service.CommentService;
import com.yuliang.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2023-09-23 14:49:00
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private UserMapper userMapper;

    @Override
    public R getCommentList(String type,Long articleId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Objects.nonNull(articleId) && articleId > 0,Comment::getArticleId,articleId);
        queryWrapper.eq(Comment::getType, type);//评论
        queryWrapper.eq(Comment::getRootId,-1);//根评论

        IPage<Comment> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<Comment> records = page.getRecords();
        //setUserName
        List<CommentVo> commentVos = toSetUserName(records);

        /**
         * 这是我第一次实现评论下的评论的代码
         */
        //查询所有非根评论
//        LambdaQueryWrapper<Comment> queryWrapper2 = new LambdaQueryWrapper();
//        queryWrapper2.ne(Comment::getRootId,-1);//ne()不等于
//        queryWrapper2.eq(Comment::getType,SystemConstants.ARTICLE_COMMENT);
//        queryWrapper2.orderByAsc(Comment::getCreateTime);
//        List<Comment> list = list(queryWrapper2);
//        //setUserName
//        List<CommentVo> commentVos2 = toSetUserName(list);

        //遍历所有根评论（遍历非根评论判断rootId在赋值给根评论的children
//        for (CommentVo commentVo:commentVos) {
//            List<CommentVo> voList = new ArrayList<>();
//            for (CommentVo commentVo2:commentVos2) {
//                if (Objects.equals(commentVo2.getRootId(), commentVo.getId())){
//                    voList.add(commentVo2);
//                }
//            }
//            commentVo.setChildren(voList);
//        }
        //子评论
        for (CommentVo commentVo:commentVos){
            commentVo.setChildren(getChildren(commentVo.getId()));
        }

        return R.okResult(new PageVo(commentVos,page.getTotal()));
    }

    /**
     * 给CommentVo设置userName属性
     * @param list
     * @return
     */
    public List<CommentVo> toSetUserName(List<Comment> list) {
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);
        for (CommentVo commentVo:commentVos) {
            commentVo.setUserName(userMapper.selectById(commentVo.getCreateBy()).getNickName());//vo
            if (commentVo.getToCommentId() != -1){//给ToCommentUserName赋值 | 不为-1才进行查询
                commentVo.setToCommentUserName(userMapper.selectById(commentVo.getToCommentUserId()).getNickName());
            }
        }
        return commentVos;
    }

    /**
     * 获取指定rootId下的评论
     * @param id
     * @return
     */
    public List<CommentVo> getChildren(Long id){
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> list = list(queryWrapper);
        List<CommentVo> voList = toSetUserName(list);//赋userName
        return voList;
    }


    @Override
    public R comment(Comment comment) {
        if (!StringUtils.hasText(comment.getContent())){//文章内容不能为空
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);
        return R.okResult();
    }
}
